package com.john.testnormal;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.john.xunhuanviewpager.R;

public class TestNormalActivity extends Activity{
	
	private ViewPager viewPager;
	private int[] imageViewIds;
	private ImageView[] imageViews;
	private PagerAdapter pagerAdapter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.test_main);
		initView();
	}
	
	private void initView() {
		imageViewIds = new int[] { R.drawable.i1, R.drawable.i2, R.drawable.i3,
				R.drawable.i4 };
		imageViews = new ImageView[imageViewIds.length];
		for (int i = 0; i < imageViewIds.length; i++) {
			imageViews[i] = new ImageView(this);
			imageViews[i].setImageResource(imageViewIds[i]);
		}
		viewPager = (ViewPager) findViewById(R.id.viewpager);
		pagerAdapter = new MyLoopViewPagerAdatper();
		viewPager.setAdapter(pagerAdapter);
	}
	
	private class MyLoopViewPagerAdatper extends PagerAdapter {

		@Override
		public int getCount() {
			return imageViews.length;
		}

		@Override
		public boolean isViewFromObject(View view, Object object) {
			return view == (View) object;
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			// super.destroyItem(container, position, object);
			container.removeView((View) object);
		}

		@Override
		public Object instantiateItem(ViewGroup container, final int position) {
			// return super.instantiateItem(container, position);
			container.addView(imageViews[position]);
			imageViews[position].setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {
					System.out.println("========>>> 点击了viewpager的第 " + position
							+ " 项");
				}
			});
			return imageViews[position];
		}
	}
}
